﻿using System;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Common;
using DAO;
using BUS;
public partial class Admin_Pages_WRCOMMENTManager : BasePage
{
    public static bool Insert;
    BUS_WRCOMMENT busWRCOMMENT = new BUS_WRCOMMENT();
    BUS_WRENTRY busWrentry = new BUS_WRENTRY();
    BUS_WRUSER busWruser = new BUS_WRUSER();
    private void Show(string Mess)
    {
        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('" + Mess + "')", true);
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Page.MaintainScrollPositionOnPostBack = true;
            pnUpdate.Visible = false;
            pnShow.Visible = true;
            BindGrid();
            BindDrop();
        }
    }
    private void BindGrid()
    {
        grvWRCOMMENT.DataSource = busWRCOMMENT.Get_Comment_List();
        grvWRCOMMENT.DataBind();
    }

    public void BindDrop()
    {
        dropIDEntry.DataSource = busWrentry._GetList().Where(q => q.Active == "Y").Select(p => new { p.ID, p.EntryName }).ToList();
        dropIDEntry.DataTextField = "EntryName";
        dropIDEntry.DataValueField = "Id";
        dropIDEntry.DataBind();
        dropIDUser.DataSource = busWruser._GetList().Where(q => q.Active == "Y").Select(p => new { p.ID, Name = p.Lastname + " " + p.Firstname }).ToList();
        dropIDUser.DataTextField = "Name";
        dropIDUser.DataValueField = "Id";
        dropIDUser.DataBind();

    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        pnUpdate.Visible = true;
        pnShow.Visible = false;
        Insert = true;
        txtCmtContent.Text = "";

    }

    protected void btnDelete_Click(object sender, EventArgs e)
    {
        if (grvWRCOMMENT.Items.Count > 0)
        {
            for (int i = 0; i < grvWRCOMMENT.Items.Count; i++)
            {
                CheckBox chkSelect = (CheckBox)grvWRCOMMENT.Items[i].FindControl("chkSelect");
                if (chkSelect.Checked)
                {
                    string Id = grvWRCOMMENT.Items[i].Cells[1].Text;
                    try
                    {
                        busWRCOMMENT._Delete(Convert.ToInt32(Id));
                    }
                    catch
                    {
                    }
                }
            }
        }
        BindGrid();
    }

    protected void btnOk_Click(object sender, EventArgs e)
    {
        if (txtCmtContent.Text.Trim().Equals(""))
        {
            Show("Bạn chưa nhập CmtContent!");
            txtCmtContent.Focus();
            return;
        }


        WRCOMMENT obj = new WRCOMMENT();
        obj.CreateDate = DateTime.Now;
        obj.IDEntry = Convert.ToInt32(dropIDEntry.SelectedValue);
        obj.IDUser = Convert.ToInt32(dropIDUser.SelectedValue);
        obj.CmtContent = txtCmtContent.Text;
        obj.Active = dropActive.SelectedValue;

        if (Insert)
        {
            busWRCOMMENT._Add(obj);
        }
        else
        {
            obj.UpdateDate = DateTime.Now;
            obj.ID = Convert.ToInt32(txtID.Text);
            busWRCOMMENT._Update(obj);
        }
        BindGrid();
        pnUpdate.Visible = false;
        pnShow.Visible = true;
        Insert = false;

    }

    protected void btnBack_Click(object sender, EventArgs e)
    {
        pnUpdate.Visible = false;
        pnShow.Visible = true;

    }

    protected void grvWRCOMMENT_ItemCommand(object source, DataGridCommandEventArgs e)
    {
        string Id = e.CommandArgument.ToString();
        if (e.CommandName == "Edit")
        {
            Insert = false;
            WRCOMMENT _WRCOMMENT = busWRCOMMENT._GetObject(Convert.ToInt32(Id));
            txtID.Text = Id;
            try
            {
                dropIDEntry.SelectedValue = _WRCOMMENT.IDEntry.ToString();
            }
            catch
            {
                Show("Entry is not active!");
            }

            try
            {
                dropIDUser.SelectedValue = _WRCOMMENT.IDUser.ToString();
            }
            catch
            {
                Show("User is not active!");
            }

            txtCmtContent.Text = _WRCOMMENT.CmtContent;
            if (_WRCOMMENT.Active.ToString() == "1")
                dropActive.SelectedValue = "Y";
            else
                dropActive.SelectedValue = "N";

            pnUpdate.Visible = true;
            pnShow.Visible = false;
        }
        else if (e.CommandName == "Delete")
        {
            busWRCOMMENT._Delete(Convert.ToInt32(Id));
            BindGrid();
        }

    }

    protected void grvWRCOMMENT_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
    {
        grvWRCOMMENT.CurrentPageIndex = e.NewPageIndex;
        BindGrid();
    }
}
